#include "qgautil.h"

/********************************************************
CACRecord
********************************************************/
CACRecord::CACRecord()
{
    m_ulTotalNum = 0;
    m_ulFinishNum = 0;
    m_ulUserInNum = 0;
    m_ulTipNum = 0;
    m_ulTime = 0;
    m_ulCorrectNum = 0;
}

CACRecord::~CACRecord()
{

}

void CACRecord::Reset()
{
    m_ulTotalNum = 0;
    m_ulFinishNum = 0;
    m_ulUserInNum = 0;
    m_ulTipNum = 0;
    m_ulTime = 0;
    m_ulCorrectNum = 0;
}

double CACRecord::Accuracy()
{
    double dRet = 0.0;
    if (m_ulFinishNum > 0 &&  m_ulUserInNum > 0)
    {
        dRet = m_ulCorrectNum / (double)m_ulUserInNum;
    }

    return dRet*100;
}

double CACRecord::FinishRate()
{
    double dRet = 0.0;
    if (m_ulFinishNum > 0 &&  m_ulTotalNum > 0)
    {
        dRet = m_ulFinishNum / (double)m_ulTotalNum;
    }

    return dRet*100;
}

double CACRecord::Speed()
{
    double dRet = 0.0;
    if (m_ulTime > 0 &&  m_ulUserInNum > 0)
    {
        dRet = m_ulUserInNum / (double)m_ulTime;
    }

    return dRet;
}

unsigned long CACRecord::Score()
{
    double dSpeed = this->Speed();
    double dFinishedRate = this->FinishRate();
    double dAccuracy = this->Accuracy();
    if (dSpeed == 0 || dFinishedRate == 0 || dAccuracy == 0)
    {
        return 0;
    }
    unsigned long ulRet = dSpeed*50+dFinishedRate*5 + dAccuracy* 5 - m_ulTipNum * 20;
    qDebug("s:%0.2f f:%0.2f a:%0.2f %ld",dSpeed, dFinishedRate, dAccuracy, ulRet);

    return ulRet;
}

/********************************************************
QGAUTIL
********************************************************/
QGaUtil* QGaUtil::m_instance = NULL;

QGaUtil::QGaUtil()
{
    int nIndex = 0;

    m_cLetters[nIndex++] = 'm';
    m_cLetters[nIndex++] = 'h';
    m_cLetters[nIndex++] = 'f';
    m_cLetters[nIndex++] = 'j';
    m_cLetters[nIndex++] = 'e';
    m_cLetters[nIndex++] = '[';
    m_cLetters[nIndex++] = 'p';
    m_cLetters[nIndex++] = 'o';
    m_cLetters[nIndex++] = ']';

    m_cLetters[nIndex++] = '`';
    m_cLetters[nIndex++] = 'v';
    m_cLetters[nIndex++] = '.';
    m_cLetters[nIndex++] = 's';
    m_cLetters[nIndex++] = 'a';
    m_cLetters[nIndex++] = 'w';
    m_cLetters[nIndex++] = 'q';
    m_cLetters[nIndex++] = '\'';
    m_cLetters[nIndex++] = '/';
    m_cLetters[nIndex++] = 'u';
    m_cLetters[nIndex++] = 'y';
    m_cLetters[nIndex++] = 't';
    m_cLetters[nIndex++] = 'r';
    m_cLetters[nIndex++] = ';';
    m_cLetters[nIndex++] = 'g';
    m_cLetters[nIndex++] = 'l';
    m_cLetters[nIndex++] = 'k';
    m_cLetters[nIndex++] = ',';
    m_cLetters[nIndex++] = 'i';
    m_cLetters[nIndex++] = 'b';
    m_cLetters[nIndex++] = 'd';
    m_cLetters[nIndex++] = 'n';
    m_cLetters[nIndex++] = 'x';
    m_cLetters[nIndex++] = 'c';
    m_cLetters[nIndex++] = 'z';

    nIndex = 0;
    m_cSymbolNum[nIndex++] = '~';
    m_cSymbolNum[nIndex++] = '1';
    m_cSymbolNum[nIndex++] = '2';
    m_cSymbolNum[nIndex++] = '3';
    m_cSymbolNum[nIndex++] = '4';
    m_cSymbolNum[nIndex++] = '5';
    m_cSymbolNum[nIndex++] = '6';
    m_cSymbolNum[nIndex++] = '7';
    m_cSymbolNum[nIndex++] = '8';
    m_cSymbolNum[nIndex++] = '9';
    m_cSymbolNum[nIndex++] = '0';
    m_cSymbolNum[nIndex++] = 'Q';
    m_cSymbolNum[nIndex++] = 'W';
    m_cSymbolNum[nIndex++] = 'E';
    m_cSymbolNum[nIndex++] = 'R';
    m_cSymbolNum[nIndex++] = 'T';
    m_cSymbolNum[nIndex++] = 'Y';
    m_cSymbolNum[nIndex++] = 'U';
    m_cSymbolNum[nIndex++] = 'I';
    m_cSymbolNum[nIndex++] = 'O';
    m_cSymbolNum[nIndex++] = 'P';
    m_cSymbolNum[nIndex++] = '{';
    m_cSymbolNum[nIndex++] = '}';
    m_cSymbolNum[nIndex++] = 'A';
    m_cSymbolNum[nIndex++] = 'S';
    m_cSymbolNum[nIndex++] = 'D';
    m_cSymbolNum[nIndex++] = 'F';
    m_cSymbolNum[nIndex++] = 'G';
    m_cSymbolNum[nIndex++] = 'H';
    m_cSymbolNum[nIndex++] = 'J';
    m_cSymbolNum[nIndex++] = 'K';
    m_cSymbolNum[nIndex++] = 'L';
    m_cSymbolNum[nIndex++] = ':';
    m_cSymbolNum[nIndex++] = '"';
    m_cSymbolNum[nIndex++] = 'Z';
    m_cSymbolNum[nIndex++] = 'X';
    m_cSymbolNum[nIndex++] = 'C';
    m_cSymbolNum[nIndex++] = 'V';
    m_cSymbolNum[nIndex++] = 'B';
    m_cSymbolNum[nIndex++] = 'N';
    m_cSymbolNum[nIndex++] = 'M';
    m_cSymbolNum[nIndex++] = '<';
    m_cSymbolNum[nIndex++] = '>';
    m_cSymbolNum[nIndex++] = '?';
}

void QGaUtil::GetArabic101KeyChar(QChar ch, QString &sArabic)
{
        QString szBuf;        

        switch(ch.unicode())
        {
        case '0':
                szBuf[0] = 0x0660;
                break;
        case '1':
                szBuf[0] = 0x0661;
                break;
        case '2':
                szBuf[0] = 0x0662;
                break;
        case '3':
                szBuf[0] = 0x0663;
                break;
        case '4':
                szBuf[0] = 0x0664;
                break;
        case '5':
                szBuf[0] = 0x0665;
                break;
        case '6':
                szBuf[0] = 0x0666;
                break;
        case '7':
                szBuf[0] = 0x0667;
                break;
        case '8':
                szBuf[0] = 0x0668;
                break;
        case '9':
                szBuf[0] = 0x0669;
                break;
        case 'a':
                szBuf[0] = 0x0634;
                break;
        case 'A':
                szBuf[0] = 0x0650;
                break;
        case 'b':
                szBuf[0] = 0x0644;
                szBuf[1] = 0x0627;
                break;
        case 'B':
                szBuf[0] = 0x0644;
                szBuf[1] = 0x0622;
                break;
        case 'c':
                szBuf[0] = 0x0624;
                break;
        case 'C':
                szBuf[0] = 0x007D;
                break;
        case 'd':
                szBuf[0] = 0x064a;
                break;
        case 'D':
                szBuf[0] = 0x005D;
                break;
        case 'e':
                szBuf[0] = 0x062b;
                break;
        case 'E':
                szBuf[0] = 0x064F;
                break;
        case 'f':
                szBuf[0] = 0x0628;
                break;
        case 'F':
                szBuf[0] = 0x005B;
                break;
        case 'g':
                szBuf[0] = 0x0644;
                break;
        case 'G':
                szBuf[0] = 0x0644;
                szBuf[1] = 0x0623;
                break;
        case 'h':
                szBuf[0] = 0x0627;
                break;
        case 'H':
                szBuf[0] = 0x0623;
                break;
        case 'i':
                szBuf[0] = 0x0647;
                break;
        case 'I':
                szBuf[0] = 0x00F7;
                break;
        case 'j':
                szBuf[0] = 0x062A;
                break;
        case 'J':
                szBuf[0] = 0x0640;
                break;
        case 'k':
                szBuf[0] = 0x0646;
                break;
        case 'K':
                szBuf[0] = 0x060C;
                break;
        case 'l':
                szBuf[0] = 0x0645;
                break;
        case 'L':
                szBuf[0] = 0x002F;
                break;
        case 'm':
                szBuf[0] = 0x0629;
                break;
        case 'M':
                szBuf[0] = 0x2019;
                break;
        case 'n':
                szBuf[0] = 0x0649;
                break;
        case 'N':
                szBuf[0] = 0x0622;
                break;
        case 'o':
                szBuf[0] = 0x062E;
                break;
        case 'O':
                szBuf[0] = 0x00D7;
                break;
        case 'p':
                szBuf[0] = 0x062D;
                break;
        case 'P':
                szBuf[0] = 0x061B;
                break;
        case 'q':
                szBuf[0] = 0x0636;
                break;
        case 'Q':
                szBuf[0] = 0x064E;
                break;
        case 'r':
                szBuf[0] = 0x0642;
                break;
        case 'R':
                szBuf[0] = 0x064C;
                break;
        case 's':
                szBuf[0] = 0x0633;
                break;
        case 'S':
                szBuf[0] = 0x064D;
                break;
        case 't':
                szBuf[0] = 0x0641;
                break;
        case 'T':
                szBuf[0] = 0x0644;
                szBuf[1] = 0x0625;
                break;
        case 'u':
                szBuf[0] = 0X0639;
                break;
        case 'U':
                szBuf[0] = 0x2018;
                break;
        case 'v':
                szBuf[0] = 0x0631;
                break;
        case 'V':
                szBuf[0] = 0x007B;
                break;
        case 'w':
                szBuf[0] = 0x0635;
                break;
        case 'W':
                szBuf[0] = 0x064B;
                break;
        case 'x':
                szBuf[0] = 0x0621;
                break;
        case 'X':
                szBuf[0] = 0x0652;
                break;
        case 'y':
                szBuf[0] = 0x063a;
                break;
        case 'Y':
                szBuf[0] = 0x0625;
                break;
        case 'z':
                szBuf[0] = 0x0626;
                break;
        case 'Z':
                szBuf[0] = 0x007E;
                break;
        case '[':
                szBuf[0] = 0x062c;
                break;
        case ']':
                szBuf[0] = 0x062F;
                break;
        case '{':
                szBuf[0] = 0x003E;
                break;
        case '}':
                szBuf[0] = 0x003C;
                break;
        case '?':
                szBuf[0] = 0x061F;
                break;
        case '/':
                szBuf[0] = 0x0638;
                break;
        case ',':
                szBuf[0] = 0x0648;
                break;
        case '<':
                szBuf[0] = 0x002C;
                break;
        case '>':
                szBuf[0] = 0x002E;
                break;
        case '.':
                szBuf[0] = 0x0632;
                break;
        case ';':
                szBuf[0] = 0x0643;
                break;
        case ':':
                szBuf[0] = 0x003A;
                break;
        case '\'':
                szBuf[0] = 0x0637;
                break;
        case'\"':
                szBuf[0] = 0x0022;
                break;
        case '!':szBuf[0] = '!';
                break;
        case '@':
                szBuf[0] =  '@';
                break;
        case '#':
                szBuf[0] =  '#';
                break;
        case '$':
                szBuf[0] =  '$';
                break;
        case '%':
                szBuf[0] =  '%';
                break;
        case '^':
                szBuf[0] =  '^';
                break;
        case '&':
                szBuf[0] =  '&';
                break;
        case '*':
                szBuf[0] =  '*';
                break;
        case ')':
                szBuf[0] =  '(';
                break;
        case '(':
                szBuf[0] =  ')';
                break;
        case '-':
                szBuf[0] =  '-';
                break;
        case '_':
                szBuf[0] =  '_';
                break;
        case '=':
                szBuf[0] =  '=';
                break;
        case '+':
                szBuf[0] =  '+';
                break;
        case '\\':
                szBuf[0] =  '\\';
                break;
        case '|':
                szBuf[0] =  '|';
                break;
        case '~':
                szBuf[0] = 0x0651;
                break;
        case '`':
                szBuf[0] = 0x0630;
                break;
        case ' ':
                szBuf[0] =  ' ';
                break;
        default:
                szBuf[0] =  ch;
        }

        sArabic = szBuf;
}

void QGaUtil::GetArabic102KeyChar(QChar ch, QString &sArabic)
{
        QString szBuf;

        switch(ch.unicode())
        {
        case '0':
                szBuf[0] = 1632;
                break;
        case '1':
                szBuf[0] = 1633;
                break;
        case '2':
                szBuf[0] = 1634;
                break;
        case '3':
                szBuf[0] = 1635;
                break;
        case '4':
                szBuf[0] = 1636;
                break;
        case '5':
                szBuf[0] = 1637;
                break;
        case '6':
                szBuf[0] = 1638;
                break;
        case '7':
                szBuf[0] = 1639;
                break;
        case '8':
                szBuf[0] = 1640;
                break;
        case '9':
                szBuf[0] = 1641;
                break;
        case 'a':
                szBuf[0] = 0x0634;
                break;
        case 'A':
                szBuf[0] = 0x0650;
                break;
        case 'b':
                szBuf[0] = 0x0644;
                szBuf[1]= 0x0627;
                break;
        case 'B':
                szBuf[0] = 0x0644;
                szBuf[1]=0x0622;
                break;
        case 'c':
                szBuf[0] = 0x0624;
                break;
        case 'C':
                szBuf[0] = 0x007D;
                break;
        case 'd':
                szBuf[0] = 0x064a;
                break;
        case 'D':
                szBuf[0] = 0x005D;
                break;
        case 'e':
                szBuf[0] = 0x062b;
                break;
        case 'E':
                szBuf[0] = 0x064F;
                break;
        case 'f':
                szBuf[0] = 1576;
                break;
        case 'F':
                szBuf[0] = 0x005B;
                break;
        case 'g':
                szBuf[0] = 0x0644;
                break;
        case 'G':
                szBuf[0] = 0x0644;
                szBuf[1]=0x0623;
                break;
        case 'h':
                szBuf[0] = 1575;
                break;
        case 'H':
                szBuf[0] = 0x0623;
                break;
        case 'i':
                szBuf[0] = 1607;
                break;
        case 'I':
                szBuf[0] = 0x00F7;
                break;
        case 'j':
                szBuf[0] = 1578;
                break;
        case 'J':
                szBuf[0] = 0x0640;
                break;
        case 'k':
                szBuf[0] = 0x0646;
                break;
        case 'K':
                szBuf[0] = 0x060C;
                break;
        case 'l':
                szBuf[0] = 0x0645;
                break;
        case 'L':
                szBuf[0] = 0x002F;
                break;
        case 'm':
                szBuf[0] = 0x0629;
                break;
        case 'M':
                szBuf[0] = 0x2019;
                break;
        case 'n':
                szBuf[0] = 0x0649;
                break;
        case 'N':
                szBuf[0] = 0x0622;
                break;
        case 'o':
                szBuf[0] = 1582;
                break;
        case 'O':
                szBuf[0] = 0x00D7;
                break;
        case 'p':
                szBuf[0] = 0x062D;
                break;
        case 'P':
                szBuf[0] = 0x061B;
                break;
        case 'q':
                szBuf[0] = 0x0636;
                break;
        case 'Q':
                szBuf[0] = 0x064E;
                break;
        case 'r':
                szBuf[0] = 0x0642;
                break;
        case 'R':
                szBuf[0] = 0x064C;
                break;
        case 's':
                szBuf[0] = 0x0633;
                break;
        case 'S':
                szBuf[0] = 0x064D;
                break;
        case 't':
                szBuf[0] = 0x0641;
                break;
        case 'T':
                szBuf[0] = 0x0644;
                szBuf[1]=0x0625;
                break;
        case 'u':
                szBuf[0] = 1593;
                break;
        case 'U':
                szBuf[0] = 0x2018;
                break;
        case 'v':
                szBuf[0] = 0x0631;
                break;
        case 'V':
                szBuf[0] = 0x007B;
                break;
        case 'w':
                szBuf[0] = 0x0635;
                break;
        case 'W':
                szBuf[0] = 0x064B;
                break;
        case 'x':
                szBuf[0] = 1569;
                break;
        case 'X':
                szBuf[0] = 0x652;
                break;
        case 'y':
                szBuf[0] = 1594;
                break;
        case 'Y':
                szBuf[0] = 0x625;
                break;
        case 'z':
                szBuf[0] = 0x0626;
                break;
        case 'Z':
                szBuf[0] = 0x007E;
                break;
        case '[':
                szBuf[0] = 0x062c;
                break;
        case ']':
                szBuf[0] = 0x062F;
                break;
        case '{':
                szBuf[0] = 0x003E;
                break;
        case '}':
                szBuf[0] = 0x003C;
                break;
        case '?':
                szBuf[0] = 0x061F;
                break;
        case '/':
                szBuf[0] = 0x0638;
                break;
        case ',':
                szBuf[0] = 0x0648;
                break;
        case '<':
                szBuf[0] = 0x002C;
                break;
        case '>':
                szBuf[0] = 0x002E;
                break;
        case '.':
                szBuf[0] = 0x0632;
                break;
        case ';':
                szBuf[0] = 0x0643;
                break;
        case ':':
                szBuf[0] = 0x003A;
                break;
        case '\'':
                szBuf[0] = 0x0637;
                break;
        case'\"':
                szBuf[0] = 0x0022;
                break;
        case '!':szBuf[0] = '!';
                break;
        case '@':
                szBuf[0] =  '@';
                break;
        case '#':
                szBuf[0] =  '#';
                break;
        case '$':
                szBuf[0] =  '$';
                break;
        case '%':
                szBuf[0] =  '%';
                break;
        case '^':
                szBuf[0] =  '^';
                break;
        case '&':
                szBuf[0] =  '&';
                break;
        case '*':
                szBuf[0] =  '*';
                break;
        case ')':
                szBuf[0] =  '(';
                break;
        case '(':
                szBuf[0] =  ')';
                break;
        case '-':
                szBuf[0] =  '-';
                break;
        case '_':
                szBuf[0] =  '_';
                break;
        case '=':
                szBuf[0] =  '=';
                break;
        case '+':
                szBuf[0] =  '+';
                break;
        case '\\':
                szBuf[0] = 0x0630;
                break;
        case '|':
                //szBuf[0] =  '|';
                szBuf[0] = 0x0651;
                break;
        case '~':
                szBuf[0] = '~';
                break;
        case '`':
                //szBuf[0] = 0x0630;
                szBuf[0] = '`';
                break;
        case ' ':
                szBuf[0] =  ' ';
                break;
        default:
                szBuf[0] =  ch;
        }

        sArabic = szBuf;
}

QString QGaUtil::TransEn2Ar(QString &sEn, bool b101Key /*= true*/)
{
    QString sDst = "";
    for (int i = 0; i < sEn.length(); i ++)
    {
        QString  sArTmp;
        if (b101Key)
        {
            GetArabic101KeyChar(sEn[i], sArTmp);
        }
        else
        {
            GetArabic102KeyChar(sEn[i], sArTmp);
        }
        sDst.append(sArTmp);
    }

    return sDst;
}
